<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="任务单号" prop="workPlanId">
        <el-input v-model="formData.workPlanId" placeholder="请输入任务单号" />
      </el-form-item>
      <el-form-item label="制单人" prop="biller">
        <el-input v-model="formData.biller" placeholder="请输入制单人" />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="formData.materialName" placeholder="请输入物料名称" />
      </el-form-item>
      <el-form-item label="物料代码" prop="materialId">
        <el-input v-model="formData.materialId" placeholder="请输入物料代码" />
      </el-form-item>
      <el-form-item label="仓库代码" prop="stockId">
        <el-input v-model="formData.stockId" placeholder="请输入仓库代码" />
      </el-form-item>
      <el-form-item label="仓库名称" prop="stockName">
        <el-input v-model="formData.stockName" placeholder="请输入仓库名称" />
      </el-form-item>
      <el-form-item label="计量单位编号" prop="unitId">
        <el-input v-model="formData.unitId" placeholder="请输入计量单位编号" />
      </el-form-item>
      <el-form-item label="计量单位名称" prop="unitName">
        <el-input v-model="formData.unitName" placeholder="请输入计量单位名称" />
      </el-form-item>
      <el-form-item label="投料单变更次数" prop="changeTimes">
        <el-input v-model="formData.changeTimes" placeholder="请输入投料单变更次数" />
      </el-form-item>
      <el-form-item label="投料单行数" prop="entryId">
        <el-input v-model="formData.entryId" placeholder="请输入投料单行数" />
      </el-form-item>
      <el-form-item label="单位用量" prop="unitNum">
        <el-input v-model="formData.unitNum" placeholder="请输入单位用量" />
      </el-form-item>
      <el-form-item label="标准用量" prop="standardNum">
        <el-input v-model="formData.standardNum" placeholder="请输入标准用量" />
      </el-form-item>
      <el-form-item label="已领数量" prop="hasGetNum">
        <el-input v-model="formData.hasGetNum" placeholder="请输入已领数量" />
      </el-form-item>
      <el-form-item label="投料日期" prop="bomDate">
        <el-date-picker
          v-model="formData.bomDate"
          type="date"
          value-format="x"
          placeholder="选择投料日期"
        />
      </el-form-item>
      <el-form-item label="投料单编号" prop="bomId">
        <el-input v-model="formData.bomId" placeholder="请输入投料单编号" />
      </el-form-item>
      <el-form-item label="投料单内码" prop="bomInterId">
        <el-input v-model="formData.bomInterId" placeholder="请输入投料单内码" />
      </el-form-item>
       <el-form-item label="备注" prop="note">
        <el-input v-model="formData.note" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { BaseWorkPlanApi } from '@/api/mes/baseworkplan'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  workPlanId: undefined,
  biller: undefined,
  materialName: undefined,
  materialId: undefined,
  stockId: undefined,
  stockName: undefined,
  unitId: undefined,
  unitName: undefined,
  changeTimes: undefined,
  entryId: undefined,
  unitNum: undefined,
  standardNum: undefined,
  hasGetNum: undefined,
  bomDate: undefined,
  bomId: undefined,
  bomInterId: undefined,
  workPlanInterId: undefined,
  note: undefined,
})
const formRules = reactive({
  workPlanId: [{ required: true, message: '任务单号不能为空', trigger: 'blur' }],
  biller: [{ required: true, message: '制单人不能为空', trigger: 'blur' }],
  materialName: [{ required: true, message: '物料名称不能为空', trigger: 'blur' }],
  materialId: [{ required: true, message: '物料代码不能为空', trigger: 'blur' }],
  stockId: [{ required: true, message: '仓库代码不能为空', trigger: 'blur' }],
  stockName: [{ required: true, message: '仓库名称不能为空', trigger: 'blur' }],
  unitId: [{ required: true, message: '计量单位编号不能为空', trigger: 'blur' }],
  unitName: [{ required: true, message: '计量单位名称不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, workPlanInterId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.workPlanInterId = workPlanInterId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BaseWorkPlanApi.getBaseBomChild(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await BaseWorkPlanApi.createBaseBomChild(data)
      message.success(t('common.createSuccess'))
    } else {
      await BaseWorkPlanApi.updateBaseBomChild(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    workPlanId: undefined,
    biller: undefined,
    materialName: undefined,
    materialId: undefined,
    stockId: undefined,
    stockName: undefined,
    unitId: undefined,
    unitName: undefined,
    changeTimes: undefined,
    entryId: undefined,
    unitNum: undefined,
    standardNum: undefined,
    hasGetNum: undefined,
    bomDate: undefined,
    bomId: undefined,
    bomInterId: undefined,
    workPlanInterId: undefined,
    note: undefined,
  }
  formRef.value?.resetFields()
}
</script>